<template>
	<view>
		<!-- 头部 -->
		<view class="top-header" :style="{position:isFixed?'fixed':'static'}">
			<view class="header">
				<icon type="search" size="16" color="#bbb">
				</icon>
				<input type="text" v-model="keyword" confirm-type="search" @confirm="search"></input>
			</view>

			<!-- 过滤菜单 -->
			<view class="filter-menu">
				<view class="active" v-for="(item, index) in 3" :key="index">销量
				</view>
			</view>
		</view>

		<!-- 商品列表 -->
		<view class="goods-list" :style="{marginTop:isFixed?'220rpx':'0'}">
			<view class="goods" v-for="(item, index) in goodsList" :key="index">
				<image :src="item.goods_small_logo" alt="">
				</image>
				<view class="right">
					<view class="goods-name text-line2">{{item.goods_name}}</view>
					<view class="price">￥<text>{{item.goods_price}}</text>.00</view>
				</view>
			</view>
		</view>
		<!-- 最后一页提示 -->
		<view class="btm-line" v-if="isLastPage">
			---我是有底线的---
		</view>
	</view>
</template>

<script>
	// 页容量,一般不会改变,所以应该作为常量
	const PAGE_SIZE = 6

	export default {
		data() {
			return {
				keyword: '',
				// 商品列表
				goodsList: [],
				// 页码
				pageNum: 1,
				// 是否是最后一页
				isLastPage: false,
				// 是否正在请求
				isRequesting: false,
				// 头部固定
				isFixed: true
			}
		},
		onLoad(options) {
			this.keyword = options.catName
			this.queryGoodsList()
		},
		// 下拉会触发
		onPullDownRefresh() {
			this.isFixed = false
			// console.log('ok')
			// 请求第一页数据
			// this.pageNum = 1
			// // 清空列表
			// this.goodsList = []
			// this.queryGoodsList()
			this.search()
		},
		// 监听页面滚动
		onPageScroll() {
			this.isFixed = true
		},
		// 上拉加载
		onReachBottom() {
			// 如果不是最后一页,才发请求
			if (!this.isLastPage) {
				// 加载下一页数据，追加到列表后面
				this.pageNum++
				this.queryGoodsList()
			}
		},
		methods: {
			// 根据输入框点键盘右下角按钮时触发搜索
			search() {
				this.pageNum = 1
				this.goodsList = []
				this.queryGoodsList()
			},
			// 根据关键字查询商品列表
			async queryGoodsList() {
				if (this.isRequesting) {
					return
				}

				// 请求之前,先设置请求中
				this.isRequesting = true
				let data = await this.$request({
					url: '/api/public/v1/goods/search',
					data: {
						query: this.keyword,
						pagenum: this.pageNum,
						pagesize: PAGE_SIZE
					},
					// 请求不需要Loading
					isShowLoading: false
				})
				// 请求结束后,释放请求中
				this.isRequesting = false

				// 主动停止下拉动画
				uni.stopPullDownRefresh()
				// 新的数据追加到原列表后面
				// this.goodsList = [...this.goodsList, ...data.goods]
				this.goodsList.push(...data.goods)
				// 对比data.total和this.goodsList长度
				if (data.total === this.goodsList.length) {
					this.isLastPage = true
				}
			}
		}
	}
</script>

<style lang="less">
	.top-header {
		position: static;
		top: 0;
		width: 100%;
		background-color: #fff;
	}

	.goods-list {
		margin-top: 220rpx;
	}

	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.filter-menu {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 100rpx;

		view.active {
			color: #EB4450;
		}
	}

	.goods {
		border-top: 1rpx solid #ddd;
		height: 260rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		display: flex;
		align-items: center;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.right {
			flex: 1;
			margin-left: 26rpx;

			.price {
				color: #eb4450;
				font-size: 24rpx;
				margin-top: 80rpx;

				text {
					font-size: 36rpx;
				}
			}
		}
	}

	.btm-line {
		text-align: center;
	}
</style>
